package com.wcar.vipmanagement.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcar.common.entity.Car;
import com.wcar.common.entity.CarStore;
import com.wcar.common.entity.FrontUser;
import com.wcar.common.util.WebUtil;
import com.wcar.common.vo.Result;
import com.wcar.vipmanagement.dao.CarDao;
import com.wcar.vipmanagement.dao.CarStoreMapper;
import com.wcar.vipmanagement.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 21147
* @description 针对表【tb_car】的数据库操作Service实现
* @createDate 2024-05-24 10:02:19
*/
@Service
public class CarServiceImpl implements CarService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource
    private CarDao carDao;
    @Resource
    private CarStoreMapper carStoreMapper;
    @Override
    public Result<List<Car>> selectCar() {
        String token = WebUtil.getToken();
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get("font:login:" + token);
        FrontUser frontUser = JSON.parseObject(s, FrontUser.class);
        QueryWrapper<Car> carQueryWrapper=new QueryWrapper<>();
        carQueryWrapper.eq("user_id",frontUser.getUserId());
        List<Car> cars = carDao.selectList(carQueryWrapper);
        for (Car c: cars) {
            c.setUserName(frontUser.getUserName());
            c.setPhone(frontUser.getPhone());
            String carNo = c.getCarNo();
            QueryWrapper<CarStore> wrapper=new QueryWrapper<>();
            wrapper.eq("car_no",carNo);
            Long aLong = carStoreMapper.selectCount(wrapper);
            c.setStoreNumber(aLong.intValue());
        }
        return new Result<>(200,"获取车辆成功",cars);
    }

    @Override
    public Result<Car> findCar(String carNo) {
        QueryWrapper<Car> carQueryWrapper=new QueryWrapper<>();
        carQueryWrapper.eq("car_no",carNo);
        Car car = carDao.selectOne(carQueryWrapper);
        return new Result<>(200,"成功",car);
    }
}




